//
//  CityViewController.m
//  WE
//
//  Created by apple on 15/11/19.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "CityViewController.h"
#import "Size.h"
#import "ProvinceTableViewCell.h"
#import "RegistrationViewController.h"
#import "DoubleDeckView.h"
#import "CityModel.h"
#import "CellAddView.h"


@interface CityViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    DoubleDeckView * doubleV;
    CellAddView *view;
}

@property (nonatomic, strong)NSDictionary *pDic;
@property (nonatomic, strong)NSArray *cArray;


@end

@implementation CityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UINavigationBar *naBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, WindowWidth, 44)];
    
    naBar.tintColor = [UIColor blackColor];
    naBar.backgroundColor = [UIColor whiteColor];
   // naBar.barTintColor = [UIColor blackColor];
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom]
    ;
    //cancelButton.backgroundColor = [UIColor whiteColor];
    cancelButton.frame = CGRectMake(0, 0, 50, 44);
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:naBar];
    [naBar addSubview:cancelButton];
    UILabel *itemLabel = [[UILabel alloc] initWithFrame:CGRectMake((WindowWidth-80)/2, 0, 80, 44)];
    itemLabel.text = @"选择地区";
    itemLabel.textColor = [UIColor blackColor];
    [naBar addSubview:itemLabel];
   
    self.cArray = @[@"北京",@"北京市",@"天津市",@"上海市",@"广东省",@"北京市",@"天津市",@"上海市",@"广东省",@"北京市",@"天津市",@"上海市",@"广东省"];
    self.pDic = @{@"北京" : self.cArray, @"上海" : self.cArray,@"天津":self.cArray,@"广东" : self.cArray, @"深圳" : self.cArray,@"天津市":self.cArray};
    
    self.cArray = [self.pDic allKeys];
    [self setupTableView];
    
}

- (void)setupTableView{
    doubleV = [[DoubleDeckView alloc] initWithFrame:CGRectMake(0, 64, WindowWidth, WindowHeight - 64)];
    [self.view addSubview:doubleV];
    doubleV.fatherTable.delegate = self;
    doubleV.fatherTable.dataSource = self;
    doubleV.sonTable.delegate = self;
    doubleV.sonTable.dataSource = self;
    
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth/2, 50*Height)];
//    view.backgroundColor = [UIColor whiteColor];
//    
//    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 22.5*Height, 5, 5*Height)];
//    imageV.image = [UIImage imageNamed:@"right"];
//    
//    [view addSubview:imageV];
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 10*Height, 80, 30*Height)];
//    NSArray *array = self.pDic.allKeys;
//    
//    label.textColor = [UIColor blackColor];
//    [view addSubview:label];
    view = [[CellAddView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth/2, 50*Height)];
    
//    self.provinceTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WindowWidth/2, WindowHeight-64)];
//    [self.view addSubview:self.provinceTable];
//    
//    self.provinceTable.dataSource = self;
//    self.provinceTable.delegate = self;
//    self.provinceTable.backgroundColor= [UIColor  colorWithRed:0.9273 green:0.9273 blue:0.9273 alpha:1.0];
//    self.provinceTable.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
//    
//    self.cityTalbe = [[UITableView alloc] initWithFrame:CGRectMake(WindowWidth/2 +1, 64, WindowWidth / 2 -1, WindowHeight - 64)];
//    [self.view addSubview:self.cityTalbe];
//    self.cityTalbe.delegate = self;
//    self.cityTalbe.dataSource = self;
    
}


- (void)back{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50*Height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
        
    
    
    static NSString *pCellString = @"provinceCell";
    ProvinceTableViewCell *pCell = [tableView dequeueReusableCellWithIdentifier:pCellString];
    if (!pCell) {
        pCell = [[ProvinceTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:pCellString];
        
        
                                    
                                    
    }
    
    
    if (tableView == doubleV.fatherTable) {
        
        pCell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSArray *array = self.pDic.allKeys;
        
        pCell.provinceLabel.text = array[indexPath.row];
        
        
        
        pCell.backgroundColor = [UIColor  colorWithRed:0.9273 green:0.9273 blue:0.9273 alpha:1.0];
    }else if(tableView == doubleV.sonTable){
        //NSArray *array = [self.pDic valueForKey:@"北京"];
        pCell.provinceLabel.text = self.cArray[indexPath.row];
    }
                                
        
        return pCell;
    
    
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == doubleV.fatherTable) {
        self.cArray = [self.pDic valueForKey:self.pDic.allKeys[indexPath.row]];
        UITableViewCell *cell = tableView.visibleCells[indexPath.row];
        NSArray *array = self.pDic.allKeys;
        view.label.text =  array[indexPath.row];
        [cell addSubview:view];
        //[tableView.visibleCells[indexPath.row] setBackgroundColor:[UIColor whiteColor]];
        
        
        [doubleV.sonTable reloadData];
    } else {
        CityModel *handle = [CityModel mainCityModel];
        
        handle.str = self.cArray[indexPath.row];
        [self dismissViewControllerAnimated:YES completion:^{
            
            
            
            
            
        }];
        
        
        
    }
    
}



- (NSInteger)numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == doubleV.fatherTable) {
        return self.pDic.allKeys.count;
    } else {
        return self.cArray.count;
    }
    
    
    
    
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
